const filter = require('../../../utils/filter')
const { getPPP,getSecretList } = require('../../../api/home')
const app = getApp()
import * as echarts from '../../../components/ec-canvas/echarts.min.js' // 这个是自己实际的目录
async function initChart(canvas, width, height, dpr) { // 这部分是固定的不需要 是通用的
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);
  const res = await getPPP()
  const data = (res.data || []).map(item => {
    const { qq, score } = item
    return { value: score, name: qq.slice(0,4) }
  })
  var option = {
    tooltip: {
      trigger: 'item'
    },
  
    series: [
      {
        name: 'ppp',
        type: 'pie',
        radius: ['100%', '70%'],  
        selectedMode: 'single',
        selectedOffset: 30,
        clockwise: true,
        itemStyle: {
          borderRadius: 5,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          fontSize: 12,
          color: '#235894'
        },
        labelLine: {
          length:10,
          length2:5,
          smooth:true,
          lineStyle: {
            color: '#000',
          }
        },
        emphasis: {
          label: {
            show: false,
            fontSize: 40,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data
      }
    ]
  };
  chart.setOption(option);
  return chart;
}

Page(filter.loginCheck({
  data: {
    ec: {
      onInit: initChart
    },
    height: '',
    secretList: [],
    userInfo: {},
    likeImg: 1,
    likeStatu: false,
    navList: [{
      label: '看新闻',
      icon: '../../../img/m1.png',
      url: '../news/news'
    }, {
      label: '听音乐',
      icon: '../../../img/m6.png',
      url: '../musics/musics'
    }, {
      label: '查快递',
      icon: '../../../img/m7.png',
      url: '../courier/courier'
    }, {
      label: '查天气',
      icon: '../../../img/m5.png',
      url: '../weather/weather'
    },]
  },
  //轮播图点击事件

  linkTo(e) {
    const url = e.currentTarget.dataset.url
    wx.navigateTo({ url })
  },
  //点赞按钮
  likeTap(e) {
    const {id,like,index} = e.currentTarget.dataset.item
    const item = this.data.secretList.find(item=>item.id === id)
    item.likeThis = !item.likeThis
    item.likeThis?item.likes++:item.likes--
    this.setData({
      secretList: this.data.secretList
    })
  },
  onLoad() {
    this.getWinHeight()
  },
  onShow() {
    this.fetchSecretData()
  },
  onReady() { },
  //获取窗口高度
  getWinHeight() {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight - 245
        })
      },
    })
  },
  //获取校园密语
  async fetchSecretData(){
    try{
      const res = await getSecretList()
      this.setData({
        secretList:res.data.map(item=>({...item,likeThis:false}))
      })
    }catch(e){
      console.log(e);
    }
  }
}))